﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.eforceglobal.crux.DTOs;
using System.Collections.Generic;
using com.eforceglobal.crux.views.Administration;
using com.eforceglobal.crux.controllers.Administration;
using com.eforceglobal.crux.views.Enums;
using com.eforceglobal.crux.security;
using com.eforceglobal.crux.BusinessFacades;
using com.eforceglobal.crux.DTOs.Configuration;

public partial class Administration_ManageUserDetails : BaseView, IManageUserDetails
{
    ManageUserDetailsController _controller = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new ManageUserDetailsController(this);
        _controller.InitView(IsPostBack);

    }

    #region IManageUserDetails Members

    public UserDetailsInformation ConcernedUserDetails
    {
        get
        {
            return GetUserDetails();
        }
        set
        {
            PopulateRoleValues(value.Roles);
            PopulateDepartmentValues(value.Departments);
            PopulateCompaniesToRepresentValues(value.CompaniesToRepresent);
            PopulateLocationsToRepresentValues(value.LocationsToRepresent);
        }
    }

    
    private void PopulateRoleValues(List<RoleInformation> list)
    {
        foreach (ListItem item in chkRoleList.Items)
        {
            if (list.SingleOrDefault(p => p.RoleID == int.Parse(item.Value)) != null)
                item.Selected = true;
        }
    }

    
    private void PopulateDepartmentValues(List<DepartmentInformation> list)
    {
        foreach (ListItem item in chkDepartmentList.Items)
        {
            if (list.SingleOrDefault(p => p.DepartmentID == int.Parse(item.Value)) != null)
                item.Selected = true;
        }
    }

    private void PopulateCompaniesToRepresentValues(List<CompanyInformation> list)
    {
        foreach (ListItem item in chkCompaniesToRepresent.Items)
        {
            if (list.SingleOrDefault(p => p.CompanyID == int.Parse(item.Value)) != null)
                item.Selected = true;
        }
    }

    private void PopulateLocationsToRepresentValues(List<LocationInformation> list)
    {
        foreach (ListItem item in chkLocationsToRepresent.Items)
        {
            if (list.SingleOrDefault(p => p.LocationID == int.Parse(item.Value)) != null)
                item.Selected = true;
        }
    }

    
    public List<RoleInformation> AllPossibleRoles
    {
        set
        {
            chkRoleList.DataSource = value;
            chkRoleList.DataTextField = "RoleNameAndDescription";
            chkRoleList.DataValueField = "RoleID";
            chkRoleList.DataBind();
        }
    }

    public List<DepartmentInformation> AllPossibleDepartments
    {
        set
        {
            chkDepartmentList.DataSource = value;
            chkDepartmentList.DataTextField = "DepartmentNameAndDescription";
            chkDepartmentList.DataValueField = "DepartmentID";
            chkDepartmentList.DataBind();
        }
    }

    public ErrorInformation ErrorDetails
    {
        get
        {
            return conErrorNotifier.ErrorInformation;
        }
        set
        {
            conErrorNotifier.ErrorInformation = value;
        }
    }

    #endregion
    protected void btnSaveChanges_Click(object sender, EventArgs e)
    {
        _controller.ModifyDetails();
    }

    private UserDetailsInformation GetUserDetails()
    {
        UserDetailsInformation details = new UserDetailsInformation();
        int concernedUserId = int.Parse(Request.QueryString["id"].ToString());
        details.ConcernedUser = _controller.GetUserInformation(concernedUserId);
        details.Roles = GetSelectedRoleInformation();
        details.Departments = GetSelectedDepartmentInformation();
        details.CompaniesToRepresent = GetSelectedCompaniesToRepresent();
        details.LocationsToRepresent = GetSelectedLocationsToRepresent();
        return details;
    }

    private List<RoleInformation> GetSelectedRoleInformation()
    {
        List<RoleInformation> roles = new List<RoleInformation>();
        foreach (ListItem item in chkRoleList.Items)
        {
            if (item.Selected)
            {
                RoleInformation roleInformation = new RoleInformation();
                roleInformation.RoleID = int.Parse(item.Value);
                roles.Add(roleInformation);
            }
        }
        return roles;
    }

    private List<DepartmentInformation> GetSelectedDepartmentInformation()
    {
        List<DepartmentInformation> departments = new List<DepartmentInformation>();
        foreach (ListItem item in chkDepartmentList.Items)
        {
            if (item.Selected)
            {
                DepartmentInformation departmentInformation = new DepartmentInformation();
                departmentInformation.DepartmentID = int.Parse(item.Value);
                departments.Add(departmentInformation);
            }
        }
        return departments;

    }

    private List<CompanyInformation> GetSelectedCompaniesToRepresent()
    {
        List<CompanyInformation> companies = new List<CompanyInformation>();
        foreach (ListItem item in chkCompaniesToRepresent.Items)
        {
            if (item.Selected)
            {
                CompanyInformation companyInformation = new CompanyInformation();
                companyInformation.CompanyID = int.Parse(item.Value);
                companies.Add(companyInformation);
            }
        }
        return companies;

    }

    private List<LocationInformation> GetSelectedLocationsToRepresent()
    {
        List<LocationInformation> companies = new List<LocationInformation>();
        foreach (ListItem item in chkLocationsToRepresent.Items)
        {
            if (item.Selected)
            {
                LocationInformation locationInformation = new LocationInformation();
                locationInformation.LocationID = int.Parse(item.Value);
                companies.Add(locationInformation);
            }
        }
        return companies;
    }

    protected void btnCancelChanges_Click(object sender, EventArgs e)
    {
        _controller.NavigateToManageUsersScreen();
    }

    #region IManageUserDetails Members


    public List<CompanyInformation> AllPossibleCompaniesToRepresent
    {
        set
        {
            chkCompaniesToRepresent.DataSource = value;
            chkCompaniesToRepresent.DataTextField = "CompanyName";
            chkCompaniesToRepresent.DataValueField = "CompanyID";
            chkCompaniesToRepresent.DataBind();
        }
    }

    public List<LocationInformation> AllPossibleLocationsToRepresent
    {
        set
        {   
            chkLocationsToRepresent.DataSource = value;
            chkLocationsToRepresent.DataTextField = "CompanyNameAndLocationName";
            chkLocationsToRepresent.DataValueField = "LocationID";
            chkLocationsToRepresent.DataBind();
        }
    }

    #endregion



    #region IManageUserDetails Members


    public bool IsCompanyRepresenativePaneVisible
    {
        set { pnlCompanyRepresentatives.Visible = value; }
    }

    public bool IsLocationRepresenativePaneVisible
    {
        set { pnlLocationRepresentatives.Visible = value; }
    }

    #endregion
}
